/**
 * 定义分页全局变量
 */
var page_Addressee = new page({
	search : searchAddressee,
});
/**
 * 定义容器全局变量
 */
var conA = new container({
	datagrid : '#listGrid',
	singleSelect : false
});

$(document).ready(function() {
	init_addUser();
	/**
	 * 添加收件人按钮设置
	 */
	$("#addAddressee").linkbutton({
		iconCls : 'icon-add',
		plain : true,
		onClick : function() {
			addAddressee();
		}
	});
/*	$('#listGrid').datagrid({
		onClickRow: function (index, row) {
			alert(row);
		}
	});
	$('#addGrid').datagrid({
		onClickRow: function (index, row) {
			alert(row);
		}
	});*/
	/**
	 * 移除收件人按钮设置
	 */
	$("#delAddressee").linkbutton({
		iconCls : 'icon-remove',
		plain : true,
		onClick : function() {
			delAddressee();
		}
	});
	/**
	 * 发送按钮设置
	 */
	$("#save_link").linkbutton({
		plain : true,
		onClick : function() {
			var bool = $("#addform").form('enableValidation').form('validate');
			if (!bool) {
				return;
			}
			addMessage();
		}
	});
	init_AddresseeType();
	$('#reContent').textbox({ editable : false});
});
/**
 * add.jsp初始化发件人
 */
function init_addUser() {
	var userId = $('#addform #userId').val();
	var userName = $('#addform #userName').val();
	$('#adduser').combobox({
		valueField : 'value',
		textField : 'text',
		data : [ {
			value : userId,
			text : userName
		} ],
		editable : false,
		onLoadSuccess : function() {
			$("#adduser").combobox("select", userId);
		}
	});
}

/**
 * add.jsp初始化收件人类型
 */
function init_AddresseeType() {
	$.ajax({
		type : "GET",
		url : ctx + "/receiveMessage/initAddresseeType",
		dataType : "json",
		data : {
			code : '001'
		},
		success : function(data) {
			if(!showMsg(data)){
				return;
			}
			$('#code').combobox({
				valueField : 'code',
				textField : 'addresseeTypeName',
				data : data.content,
				editable : false,
				onLoadSuccess : function() {
					$('#addGrid').datagrid();
				},
				onSelect : function(node) {
					searchAddressee(page_Addressee);
				},
			});
		}
	});
}

/**
 * listGrid获取查询条件
 */
function getConditionAddressee() {
	var condition = $("#addform").serializeJson()
	condition.code = $("#code").combobox("getValue");	
	return condition;
}
/**
 * listGrid根据收件人类型查询收件人
 */
function searchAddressee(pg) {
	var url = ctx + "/receiveMessage/searchAddressee";
	page_Addressee.pageSize = pg.pageSize;
	page_Addressee.pageNumber = pg.pageNumber;
	query(url, getConditionAddressee(), conA, page_Addressee);
}

/**
 * 添加收件人按钮
 * 添加listGrid收件人至addGrid 并删除listGrid
 */
function addAddressee() {
	var listGridRows = $('#listGrid').datagrid('getSelections');
	if (!listGridRows[0]) {
		return;
	}
	$.each(listGridRows, function(index, row) {
		$("#addGrid").datagrid('appendRow', {
			addresseeId : row.addresseeId,
			addresseeName : row.addresseeName,
			code : row.code,
			addresseeTypeName : row.addresseeTypeName
		});
	});
	for(var i=0;i<listGridRows.length;){
		var selectIndex = $('#listGrid').datagrid('getRowIndex', listGridRows[i]);
		$('#listGrid').datagrid('deleteRow', selectIndex);
	}
}

/**
 * 移除收件人按钮
 * 移除addGrid收件人至listGrid 
 * 并删除addGrid
 */
function delAddressee() {
	var addGrid = $('#addGrid').datagrid('getSelections');
	if (!addGrid[0]) {
		return;
	}
	$.each(addGrid, function(index, row) {
		$("#listGrid").datagrid('appendRow', {
			addresseeId : row.addresseeId,
			addresseeName : row.addresseeName,
			code : row.code,
			addresseeTypeName : row.addresseeTypeName
		});
	});
	$.each(addGrid, function(index, row) {
		var selectIndex = $('#addGrid').datagrid('getRowIndex', row);
		$('#addGrid').datagrid('deleteRow', selectIndex);
	});
}
/**
 * 写信
 */
function addMessage(){
	var getRows = $("#addGrid").datagrid("getRows");
	var addresseeIds = new Array();
	var addresseeNames = new Array();
	var codes = new Array();
	var addresseeTypeNames = new Array();
	var remind = $("input[name='remind']:checked").val();
	$.each(getRows, function(index, row) {
		addresseeIds.push(row.addresseeId);
		addresseeNames.push(row.addresseeName);
		codes.push(row.code);
		addresseeTypeNames.push(row.addresseeTypeName);
	});
	if($.trim(addresseeIds).length=='0'){
		message("收件人不能为空");
		return;
	}
	var data = {
			title : $("#title").textbox('getValue'),
			content : $("#contents").textbox('getValue'),
			userId : $("#adduser").combobox('getValue'),
			userName : $("#adduser").combobox('getText'),
			remind : remind,
			addresseeIds : addresseeIds.join(","),
			addresseeNames : addresseeNames.join(","),
			codes : codes.join(","),
			addresseeTypeNames : addresseeTypeNames.join(","),
		};
	$.ajax({
		type : "POST",
		url : ctx + "/receiveMessage/writeMessage",
		dataType : "json",
		data : data,
		success : function(data) {
			if(!showMsg(data,"发送成功")){
				return;
			}
			$('#content').panel('open').panel('refresh', ctx + '/web/message/sendMessage.jsp');
		},
	});
}

